﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Utilities;
using TCPServer;
using System.Net;

namespace ClientServerPart
{
    class Program
    {
        static Client cnt;
        static bool exitFlag = false;
        static void Main()
        {
            string svrIP;
            IPAddress serverip;
            do
            {
                Console.Write("Server IP: ");
                svrIP = Console.ReadLine();

            } while (!IPAddress.TryParse(svrIP, out serverip));

            cnt = new Client(serverip);

            Console.WriteLine("Authentication is needed.\nPlease input credentials.");
            Thread.Sleep(50); // so that all comments are displayed in the right order
            do
            {
                Console.Write("Username: ");
                string username = Console.ReadLine();
                string password = ReadPassword.GetConsoleInput("Password: ", true);
                cnt.Change(username, password);
                //Thread.Sleep(500); // wait for the server to responce 500ms
            } while (!cnt.IsAuthenticated);

            DoEnduranceTest();
            //cnt = new Client("daniel","123456");
            string message = null;
            do
            {
                message = Console.ReadLine();
                message += Client.MESSAGE_END;
                bool contain;
                Commands.checkWithParam(message, paramCommands, out contain);
                Commands.checkFor(message, commands, out contain);

                if (!contain)
                {
                    cnt.Send(message);
                }

            } while (!exitFlag);

        }

        private static void DoEnduranceTest()
        {

            Console.WriteLine("ENDURANCE START.");
            int messageSize = 375000;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < messageSize; i++)
            {
                sb.Append("a");
            }
            //
            sb.Append(DateTime.Now.ToString());
            sb.Append(Client.MESSAGE_END);
            string m = sb.ToString();

            int times = 30;
            for (int j = 0; j < times; j++)
            {
                Console.WriteLine("TRIAL " + j);
                Console.WriteLine("Message sent at: " + DateTime.Now.ToString()); 
                Console.WriteLine("With ms: " + DateTime.Now.Millisecond);
                cnt.Send(m);
                Thread.Sleep(10000);
            }
            Console.WriteLine("ENDURANCE END.");
        }

        static Dictionary<string, Delegate> commands = new Dictionary<string, Delegate>(StringComparer.OrdinalIgnoreCase)
        {
            {"print", (Action)print},
            {"help", (Action)helpPrint},
            {"exit", (Action)OnExit},
            //{"connected", (Func<bool>)cnt.IsConnected}
        };

        static Dictionary<string, Delegate> paramCommands = new Dictionary<string, Delegate>(StringComparer.OrdinalIgnoreCase){
            
            //{"messageall ", (Action<string>)cnt.Send}
        };

        static void print()
        {
            Console.WriteLine("A test print Function.");
        }

        static void helpPrint()
        {
            Console.WriteLine("Commands that can be used: ");
            Console.WriteLine("\texit  - to close the client.");
            Console.WriteLine("\tprint - a test print function.");
            Console.WriteLine("\tType anything else to be echoed back.");
        }

        static void OnExit()
        {
            //cnt.Close();
            Console.WriteLine("Exiting");
        }
    }
}
